package com.gem.controller;

import com.gem.domain.CCrCompany;
import com.gem.domain.CCrParticipant;
import com.gem.domain.Company;
import com.gem.service.CCrCompanyService;
import com.gem.service.CCrParticipantService;
import com.gem.service.CompanyService;
import com.gem.util.JSONResultUtil;
import com.gem.util.MessageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author Apolar_GuanYH-管阳红
 * Description: 参与公司控制器
 * create 2019/10/30 13:33
 * Modified By:
 */
@Controller
@RequestMapping("/cc")
public class CCrCompanyController {

    //自动注入CCrCompanyService
    private CCrCompanyService cCrCompanyService;

    @Autowired
    public void setcCrCompanyService(CCrCompanyService cCrCompanyService) {
        this.cCrCompanyService = cCrCompanyService;
    }

    //自动注入CCrParticipantService
    private CCrParticipantService cCrParticipantService;

    @Autowired
    public void setcCrParticipantService(CCrParticipantService cCrParticipantService) {
        this.cCrParticipantService = cCrParticipantService;
    }

    //自动注入JSONResultUtil
    private JSONResultUtil result;

    @Autowired
    public void setResult(JSONResultUtil result) {
        this.result = result;
    }

    //自动注入
    private CompanyService companyService;

    @Autowired
    public void setCompanyService(CompanyService companyService) {
        this.companyService = companyService;
    }

    //参与公司
    @RequestMapping("/ccShow")
    public String ccCompanyShow(String ccCrId, Model model, Integer comId, String ccrcDate, String ccrcTopic, String ccrcSales, String ccrcCourseTeacher,
                                @RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
                                @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
        try {
            List<Company> companyList = companyService.findAllCompany();
            List<CCrCompany> cCrCompanyList = cCrCompanyService.selectCCByccCrIdService(Integer.parseInt(ccCrId));
            List<CCrParticipant> cCrParticipantList;
            if (cCrCompanyList.size() > 0) {
                for (CCrCompany cCrCompany : cCrCompanyList) {
                    cCrParticipantList = cCrParticipantService.selectCPByccIdService(cCrCompany.getCcId());
                    cCrCompany.setCcpIdNum(cCrParticipantList.size());
                }
                model.addAttribute("ccList", cCrCompanyList);
            } else {
                model.addAttribute("info", "暂无信息记录");
            }
            model.addAttribute("companyList", companyList);
            model.addAttribute("ccCrId", ccCrId);
            model.addAttribute("comId",comId);
            model.addAttribute("ccrcDate",ccrcDate);
            model.addAttribute("ccrcCourseTeacher",ccrcCourseTeacher);
            model.addAttribute("ccrcTopic",ccrcTopic);
            model.addAttribute("ccrcSales",ccrcSales);
            model.addAttribute("currentPage",currentPage);
            model.addAttribute("pageSize",pageSize);
            return "company/result_company";
        } catch (Exception e) {
            e.printStackTrace();
        }
        MessageUtil.returnInfoPage("3", "/cr/crShow", "服务器异常，请联系管理员哦！", model);
        return "message";
    }

    //单个删除
    @RequestMapping(value = "/ccDelete", method = RequestMethod.POST)
    @ResponseBody
    public JSONResultUtil ccDelete(String ccId) {
        try {
            if (ccId != null && !ccId.trim().equals("")) {
                int cc_Id = Integer.parseInt(ccId);
                if (cCrCompanyService.deleteCCService(cc_Id)) {
                    result.setId(0);
                    result.setData("删除成功！");
                } else {
                    result.setId(-1);
                    result.setData("服务器异常，删除失败哦！");
                }
            } else {
                result.setId(-1);
                result.setData("服务器异常，没有提交编号参数哦！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setId(-1);
            result.setData("服务器异常，请联系管理员哦！");
        }
        return result;
    }

    //批量删除
    @RequestMapping(value = "/ccsDelete", method = RequestMethod.POST)
    @ResponseBody
    public JSONResultUtil ccsDelete(String ccIdStr) {
        try {
            String[] ccIdArr = ccIdStr.split(",");
            if (ccIdArr.length != 0) {
                if (cCrCompanyService.deleteCCsService(ccIdArr)) {
                    result.setId(0);
                    result.setData("删除成功！");
                } else {
                    result.setId(-1);
                    result.setData("删除发生异常，请检查服务器！");
                }
            } else {
                result.setId(-1);
                result.setData("没有提交编号参数哦！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setId(-1);
            result.setData("服务器异常，请联系管理员哦！");
        }
        return result;
    }

    //添加参与公司
    @RequestMapping(value = "/ccAdd", method = RequestMethod.POST)
    @ResponseBody
    public JSONResultUtil ccAdd(CCrCompany cCrCompany) {
        try {
            if (cCrCompanyService.addCCService(cCrCompany)) {
                result.setId(0);
                result.setData("添加成功！");
            } else {
                result.setId(1);
                result.setData("添加未成功哦，请重试！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setId(-1);
            result.setData("服务器异常，请联系管理员哦！");
        }
        return result;
    }

    //修改参与公司
    @RequestMapping(value = "/ccModify", method = RequestMethod.POST)
    @ResponseBody
    public JSONResultUtil ccModify(CCrCompany cCrCompany) {
        try {
            if (cCrCompanyService.modifyCCService(cCrCompany)) {
                result.setId(0);
                result.setData("修改成功！");
            } else {
                result.setId(1);
                result.setData("修改未成功哦，请重试！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setId(-1);
            result.setData("服务器异常，请联系管理员哦！");
        }
        return result;
    }
}
